<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Клиенты</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <script type="text/javascript" src="resources/js/prototype.js"></script>
    <script type="text/javascript" src="resources/js/goodwill.js"></script>
    <style type="text/css">
        .t_green {
            font-weight: bold;
            color: darkgreen;
        }

        .t_red {
            font-weight: bold;
            color: darkred;
        }

        .filter_subpanel {
            margin-left: 4px;
            margin-right: 4px;
            white-space: nowrap;
            display: inline;
        }

        input{
            width: 150px;
        }
    </style>
    <script type="text/javascript">
        function showActionsForClient(clientId, clientName) {
            $('modalShowActionForClient_clientId').value = clientId;
            $('modalShowActionForClient_clientName').value = clientName;
            Richfaces.showModalPanel('modalShowActionForClient');
        }

        function editClient(clientId) {
            _editClient(clientId);
        }

        function promptDeleteClient(clientId, companyName) {
            $('removeClientPanel_clientId').value = clientId;
            $('removeClientPanel_clientName').value = companyName;
            Richfaces.showModalPanel('removeClientPanel');
        }

        function openUser(userId) {
            _openUser(userId);
        }

        function updateView() {
            _updateView();
        }

        function createNewOrderForClient(clientId) {
            _createNewOrderForClient(clientId);
        }
    </script>
</head>
<body>
<f:view>
<a4j:form prependId="false">

    <a4j:jsFunction ajaxSingle="true" name="_updateView" reRender="tableClients,pager"/>
    <a4j:jsFunction ajaxSingle="true" name="_updatePager" reRender="pager"/>

    <a4j:jsFunction ajaxSingle="true" name="_editClient" oncomplete="go('client')">
        <a4j:actionparam name="clientId" assignTo="#{SessionBean.editableClientId}"/>
    </a4j:jsFunction>

    <a4j:jsFunction ajaxSingle="true" name="_createNewOrderForClient"
                    oncomplete="proceedModalMessagesAndGo('#{OrdersBean.facesMessagesAsString}', 'order');"
                    action="#{OrdersBean.newOrder}">
        <a4j:actionparam name="clientId" assignTo="#{OrdersBean.clientId}"/>
    </a4j:jsFunction>

    <a4j:jsFunction ajaxSingle="true" name="_openUser" oncomplete="go('users')">
        <a4j:actionparam name="userId" assignTo="#{SessionBean.usersFilter.id}"/>
        <a4j:actionparam name="user_client_id" assignTo="#{SessionBean.usersFilter.clientId}" value="-1"/>
        <a4j:actionparam name="user_parent_id" assignTo="#{SessionBean.usersFilter.parentManagerId}" value="-1"/>
        <a4j:actionparam name="user_role_id" assignTo="#{SessionBean.usersFilter.roleId}" value="-1"/>
        <a4j:actionparam name="user_fio" assignTo="#{SessionBean.usersFilter.fio}" value=""/>
    </a4j:jsFunction>
</a4j:form>

<jsp:include page="menu.jsp"/>
<jsp:include page="status.jsp"/>

<h:form style="padding-top:3px;" prependId="false" id="clientsPageForm">

    <h:panelGroup id="pager">

        <h:outputText value="Страницы (<b>#{ClientsBean.pagesCount}</b>)" escape="false" style="margin-right:5px"/>

        <a4j:commandLink action="#{ClientsBean.toBegin}" ajaxSingle="true" process="" reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/left3.png"/>
        </a4j:commandLink>
        <a4j:commandLink action="#{ClientsBean.longPrevPage}" ajaxSingle="true" process=""
                         reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/left2.png"/>
        </a4j:commandLink>
        <a4j:commandLink action="#{ClientsBean.prevPage}" ajaxSingle="true" process="" reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/left.png"/>
        </a4j:commandLink>

        <h:panelGroup id="pager_nums">
            <a4j:repeat value="#{ClientsBean.pageIndexes}" var="pageIndex">
                <a4j:commandLink id="test" action="#{ClientsBean.applyPage}" value="#{pageIndex.num}"
                                 styleClass="pager_page" reRender="tableClients,pager"
                                 rendered="#{pageIndex.num != ClientsBean.page}">
                    <a4j:actionparam name="page_id" assignTo="#{ClientsBean.page}" value="#{pageIndex.num}"/>
                </a4j:commandLink>
                <h:outputText value="#{pageIndex.num}" styleClass="pager_page_active"
                              rendered="#{pageIndex.num == ClientsBean.page}"/>
            </a4j:repeat>
        </h:panelGroup>

        <a4j:commandLink action="#{ClientsBean.nextPage}" reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/right.png"/>
        </a4j:commandLink>
        <a4j:commandLink action="#{ClientsBean.longNextPage}" reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/right2.png"/>
        </a4j:commandLink>
        <a4j:commandLink action="#{ClientsBean.toEnd}" reRender="tableClients,pager">
            <h:graphicImage value="/resources/img/arrows/right3.png"/>
        </a4j:commandLink>
    </h:panelGroup>
    <!-- ---------- -->

    <rich:spacer width="10"/>

    <h:panelGroup layout="block" style="width: 100%">
        <h:panelGrid columns="2" style="background-color: #f5f5f5;">
            <f:facet name="header">
                <h:outputText value="Фильтр"/>
            </f:facet>
            <h:panelGroup layout="block">
                <h:panelGrid cellpadding="0" cellspacing="10" columns="8">
                    <h:outputText value="Менеджер"  rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:panelGroup rendered="#{SecurityBean.isRoleAdministrator}">
                        <h:selectOneMenu value="#{SessionBean.clientsFilter.managerId}" styleClass="f_s">
                            <f:selectItems value="#{UsersBean.managersWithEmptyAsSelectItems}"/>
                        </h:selectOneMenu>
                    </h:panelGroup>
                    <h:outputText value="Наименование компании"/>
                    <h:inputText value="#{SessionBean.clientsFilter.name}" styleClass="f_tb_l"/>

                    <h:outputText value="Республика" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.repuplic}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Город" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.city}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Статус" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:selectOneMenu value="#{SessionBean.clientsFilter.clientStateId}" style="width:200px;" rendered="#{SecurityBean.isRoleAdministrator}">
                        <f:selectItems value="#{ClientsBean.clientStateList}" />
                    </h:selectOneMenu>

                    <h:outputText value="E-mail клиента" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.email}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Край" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.kray}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Улица" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.street}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Без архивных"  rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:selectBooleanCheckbox value="#{SessionBean.clientsFilter.active}" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Адрес фактический" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.addressFact}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Область" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.region}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>

                    <h:outputText value="Дом/строение/офис" rendered="#{SecurityBean.isRoleAdministrator}"/>
                    <h:inputText value="#{SessionBean.clientsFilter.house}" styleClass="f_tb_l" rendered="#{SecurityBean.isRoleAdministrator}"/>
                </h:panelGrid>
            </h:panelGroup>
            <h:panelGroup styleClass="" layout="block" style="padding-top:4px;padding-left:50px;">
                <a4j:commandButton value="Применить" action="#{ClientsBean.applyFilter}" reRender="pager,tableClients"
                                   oncomplete="_updatePager();" />
            </h:panelGroup>
        </h:panelGrid>
    </h:panelGroup>

    <%--             <!-- Фильтр -->
                <h:outputText value="Фильтр: " styleClass="b"/>

                <h:panelGroup layout="block" styleClass="filter_subpanel">
                    <h:outputText value="Наименование компании"/>
                    <h:inputText value="#{SessionBean.clientsFilter.name}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="E-mail клиента"/>
                    <h:inputText value="#{SessionBean.clientsFilter.email}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Адрес фактический"/>
                    <h:inputText value="#{SessionBean.clientsFilter.addressFact}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Республика"/>
                    <h:inputText value="#{SessionBean.clientsFilter.repuplic}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Регион"/>
                    <h:inputText value="#{SessionBean.clientsFilter.region}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Край"/>
                    <h:inputText value="#{SessionBean.clientsFilter.kray}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Город"/>
                    <h:inputText value="#{SessionBean.clientsFilter.city}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Улица"/>
                    <h:inputText value="#{SessionBean.clientsFilter.street}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Дом/строение/офис"/>
                    <h:inputText value="#{SessionBean.clientsFilter.house}" styleClass="f_tb_l"/>
                </h:panelGroup>

                <h:panelGroup layout="block" styleClass="filter_subpanel" rendered="#{SecurityBean.isRoleAdministrator}">
                    <h:outputText value="Без архивных"/>
                    <h:selectBooleanCheckbox value="#{SessionBean.clientsFilter.active}"/>
                </h:panelGroup>
                    &lt;%&ndash;<h:panelGroup layout="block" styleClass="filter_subpanel">&ndash;%&gt;
                    &lt;%&ndash;<h:outputText value="Клиентская группа"/>&ndash;%&gt;
                    &lt;%&ndash;<h:selectOneMenu value="#{SessionBean.clientsFilter.clientGroupId}" styleClass="f_s">&ndash;%&gt;
                    &lt;%&ndash;<f:selectItems value="#{ClientGroupsBean.clientGroupsWithEmptyAsSelectItems}"/>&ndash;%&gt;
                    &lt;%&ndash;</h:selectOneMenu>&ndash;%&gt;
                    &lt;%&ndash;</h:panelGroup>&ndash;%&gt;

                <rich:spacer width="10"/>
                <h:panelGroup layout="block" styleClass="filter_subpanel">
                    <h:panelGroup rendered="#{SecurityBean.isRoleAdministrator}">
                        <h:outputText value="Менеджер"/>
                        <h:selectOneMenu value="#{SessionBean.clientsFilter.managerId}" styleClass="f_s">
                            <f:selectItems value="#{UsersBean.managersWithEmptyAsSelectItems}"/>
                        </h:selectOneMenu>
                    </h:panelGroup>
                </h:panelGroup>
            <a4j:commandButton value="Применить" action="#{ClientsBean.applyFilter}" reRender="pager,tableClients"
                               oncomplete="_updatePager();" />--%>
        <!-- ------ -->



    <rich:dataTable value="#{ClientsBean.clients}" var="client" id="tableClients" styleClass="full"
                    columnClasses="clm_min,clm_min,clm_min,clm_min,clm_white,clm_white,clm_white,clm_white,clm_white,clm_white">

        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Клиенты" styleClass="table_caption"/>
                <rich:spacer width="10"/>
                <a4j:commandButton action="#"
                                   ajaxSingle="true"
                                   oncomplete="go('client');return false;"
                                   value="Создать нового клиента..."
                                   rendered="#{SecurityBean.isRoleAdministrator}">
                    <a4j:actionparam name="clientId" assignTo="#{SessionBean.editableClientId}" value="-1"/>
                </a4j:commandButton>
                <rich:spacer width="10"/>
                <a4j:commandButton id="blockRecoveryButton"
                                   value="Восстановить блокировки"
                                   action="#{ClientsBean.blockRecovery}"
                                   rendered="#{SecurityBean.isRoleAdministrator}"
                                   reRender="tableClients"
                                   oncomplete="alert('Значения блокировок успешно восстановлены');"
                                   style="width:200px;padding-left: 4px; padding-right: 4px;"/>

            </h:panelGroup>
        </f:facet>

        <%-- Действия --%>
        <rich:column>

            <!-- Редактировать клиента -->
            <h:graphicImage id="editlink" value="resources/img/edit.gif" onclick="editClient('#{client.id}')"
                            style="cursor:pointer"
                            title="Редактировать запись клиента"
                            rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleManager}"/>

            <!-- Удалить клиента -->
            <h:graphicImage id="deletelink" value="resources/img/delete.gif"
                            onclick="promptDeleteClient('#{client.id}', '#{client.companyName}')" style="cursor:pointer"
                            title="Удалить запись клиента" rendered="#{SecurityBean.isRoleAdministrator}"/>

        </rich:column>

        <rich:column rendered="false" styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Ид. в ИС"/>
            </f:facet>
            <h:outputText value="#{client.id}"/>
        </rich:column>

        <%-- ФИО --%>
        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Наименование компании"/>
            </f:facet>

            <h:outputText value="#{client.companyName}"/>
            <h:graphicImage value="/resources/img/arrow_right_blue_16.png"
                            onclick="createNewOrderForClient('#{client.id}')"
                            title="Создать заказ за этого клиента"
                            rendered="#{!client.c1CodeIsMissing && !client.managerIsMissing && client.active}"
                            style="cursor:pointer"/>
        </rich:column>

  <%--      <rich:column>
            <f:facet name="header">
                <h:outputLink value="#" onclick="go('users')" style="color:black">Пользователь</h:outputLink>
            </f:facet>
            <h:outputLink value="#user" onclick="openUser('#{client.user.id}');return false;">
                <h:outputText value="#{client.user.login}"/>
            </h:outputLink>
        </rich:column>--%>
        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Статус"/>
            </f:facet>
            <h:outputText value="#{client.clientStateAsString}"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Адрес"/>
            </f:facet>
            <h:outputText value="#{client.addressFact}"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Менеджер"/>
            </f:facet>
            <h:outputText value="#{client.manager.fio}" rendered="#{!client.managerIsMissing}"/>
            <h:outputText value="не установлен" rendered="#{client.managerIsMissing}"
                          style="color:red;font-weight:bold;font-size:80%"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Код 1С"/>
            </f:facet>
            <h:outputText value="#{client.c1ClientNum}" rendered="#{!client.c1CodeIsMissing}"/>
            <h:outputText value="не установлен" rendered="#{client.c1CodeIsMissing}"
                          style="color:red;font-weight:bold;font-size:80%"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Состояние"/>
            </f:facet>
            <h:outputText value="#{client.clientWorkStateAsString}"/>
        </rich:column>

        <rich:column id="blockColumn">
            <a4j:outputPanel layout="block">
                <rich:toolTip layout="block" value="Для разблокировки зайдите в карточку клиента"/>
            </a4j:outputPanel>
            <f:facet name="header">
                <h:outputText value="Блокировка"/>
            </f:facet>
            <h:outputText value="#{client.clientIsBlockAsString}"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Кол-во<br>заказов" escape="false"/>
            </f:facet>
            <h:outputText value="#{client.ordersCount}" id="ordersCount"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Долг<br>по банку" escape="false"/>
            </f:facet>
            <h:outputText value="#{client.debtBank}"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Долг<br>по кассе" escape="false"/>
            </f:facet>
            <h:outputText value="#{client.debtCash}"/>
        </rich:column>

        <rich:column rendered="false">
            <f:facet name="header">
                <h:outputText value="Контроль<br>суммы закупок" escape="false"/>
            </f:facet>
            <h:outputText value="да" styleClass="t_green" rendered="#{client.allowControlSum}"/>
            <h:outputText value="нет" styleClass="t_red" rendered="#{!client.allowControlSum}"/>
        </rich:column>

        <rich:column rendered="false">
            <f:facet name="header">
                <h:outputText value="Заявки<br>без согласования" escape="false"/>
            </f:facet>
            <h:outputText value="да" styleClass="t_green" rendered="#{client.allowQuickOrder}"/>
            <h:outputText value="нет" styleClass="t_red" rendered="#{!client.allowQuickOrder}"/>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}" rendered="false">
            <f:facet name="header">
                <h:outputText value="Дата<br>создания" escape="false"/>
            </f:facet>
            <h:outputText value="#{client.creationDate}" id="creationDate">
                <f:converter converterId="TimestampConverter"/>
            </h:outputText>
        </rich:column>

        <rich:column styleClass="#{SecurityBean.isRoleAdministrator && client.main ? 'blue' : ''}">
            <f:facet name="header">
                <h:outputText value="Email" escape="false"/>
            </f:facet>
            <h:outputText value="#{client.contactEmail}" id="clientSearchEmail">
            </h:outputText>
        </rich:column>
    </rich:dataTable>
</h:form>

<rich:modalPanel id="removeClientPanel" autosized="true" width="300">
    <f:facet name="header">
        <h:outputText value="Удаление клиента"/>
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage value="resources/img/close.png" styleClass="hidelink"
                        onclick="#{rich:component('removeClientPanel')}.hide()"/>
    </f:facet>

    <h:form prependId="false">

        <h:inputHidden id="removeClientPanel_clientId" value="#{ClientsBean.clientId}"/>

        <jsp:include page="messages.jsp"/>

        <h:panelGrid columns="2" cellpadding="0" cellspacing="0">
            <h:graphicImage value="/resources/img/warning_32.png"/>
            <h:panelGroup>
                <h:outputText value="Вы уверены, что хотите удалить клиента?"/>
                <h:outputText id="removeClientPanel_clientName" value=""/>
            </h:panelGroup>
        </h:panelGrid>

        <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0">
            <a4j:commandButton value="Да"
                               action="#{ClientsBean.removeClient}"
                               styleClass="btnsz"
                               oncomplete="if(#{!ClientsBean.hasFacesErrors}) {updateView(); Richfaces.hideModalPanel('removeClientPanel');}"/>

            <h:commandButton value="Нет"
                             styleClass="btnsz"
                             onclick="Richfaces.hideModalPanel('removeClientPanel');return false;"/>
        </h:panelGrid>
    </h:form>

</rich:modalPanel>

<jsp:include page="modalmessages.jsp"/>

</f:view>
</body>
</html>
